package com.zwj.service.impl;

import com.zwj.bo.*;
import com.zwj.dto.CourseMember;
import com.zwj.dto.SeeStudentQuery;
import com.zwj.dto.SelectCourseQuery;
import com.zwj.dto.SelectTeamStudent;
import com.zwj.entity.*;
import com.zwj.enums.RoleTypeEnum;
import com.zwj.enums.TeacherRole;
import com.zwj.enums.ValidEnum;
import com.zwj.enums.YesOrNoEnum;
import com.zwj.mapper.CommonMapper;
import com.zwj.mapper.CourseMapper;
import com.zwj.service.CourseService;
import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.DateUtils;
import com.zwj.utils.PaginationUtils;
import com.zwj.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CourseServiceImpI implements CourseService {
    private final CourseMapper courseMapper;
    private final CommonMapper commonMapper;
    public CourseServiceImpI(CourseMapper courseMapper,CommonMapper commonMapper) {
        this.courseMapper = courseMapper;
        this.commonMapper = commonMapper;
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public synchronized Result addCourse(AddCourseBo addCourseBo, UserEntity userEntity) {
        //数据校验
        DataCheckUtils.isNotBlank(addCourseBo.getCourseName(), "课程名称不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getProjectDate(), "课程日期不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getClassRome(), "教室不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getSignInTime(), "签到时间不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getSignOutTime(), "签退时间不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getResponsibleTeacherId(), "责任老师ID不能为空!");
        DataCheckUtils.isValid(addCourseBo.getTeacherId().length!=0, "实训老师ID不能为空!");
        DataCheckUtils.isValid(addCourseBo.getProjectDate().matches("([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))" ), "签到日期格式错误!");
        DataCheckUtils.isValid(addCourseBo.getSignInTime().matches("^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$"), "签到时间格式错误!");
        DataCheckUtils.isValid(addCourseBo.getSignOutTime().matches("^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$"), "签退时间格式错误!");
        CourseInfoEntity courseInfoEntity = commonMapper.selectCourseByNames(addCourseBo.getCourseName());
        if(courseInfoEntity!=null&&courseInfoEntity.getIsGraduation().equals(YesOrNoEnum.YES.getKey())){
            return Result.error("课程已经结束");
        }
        addCourseBo.setSignOutTime(addCourseBo.getSignOutTime()+":00");
        addCourseBo.setSignInTime(addCourseBo.getSignInTime()+":00");
        DataCheckUtils.isValid(DateUtils.compare(DateUtils.getCurrentTime(),addCourseBo.getProjectDate()+" "+addCourseBo.getSignInTime()), "课程日期不能小于当前日期!");
        DataCheckUtils.isValid(DateUtils.compare(addCourseBo.getProjectDate()+" "+addCourseBo.getSignInTime(),addCourseBo.getProjectDate()+" "+addCourseBo.getSignOutTime()), "结束时间不能小于开课时间!");

        int state = 0;
        for(String teacherId: addCourseBo.getTeacherId()){
            UserEntity userEntity1 = commonMapper.getTeacherById(teacherId);
            if(userEntity1==null){
                return Result.error("教师ID"+teacherId+"不存在");
            }
            if(teacherId.equals(addCourseBo.getResponsibleTeacherId())){
                state = 1;
            }
        }
        if(state==0){
            return Result.error("实训老师未在责任老师中");
        }
        if(courseMapper.selectCourseBy(addCourseBo)!=null){
            return Result.error("当前时间段教室被占有");
        }
        String id = "";
        if(courseInfoEntity==null) {
            CourseInfoEntity courseInfoEntity1 = new CourseInfoEntity();
            courseInfoEntity1.setCourseName(addCourseBo.getCourseName());
            courseInfoEntity1.setDelFlag(ValidEnum.EFFECTIVE.getKey());
            courseInfoEntity1.setCreateBy(userEntity.getId());
            courseInfoEntity1.setCreateTime(DateUtils.getCurrentTime());
            courseInfoEntity1.setUpdateBy(userEntity.getId());
            courseInfoEntity1.setUpdateTime(DateUtils.getCurrentTime());
            if(courseMapper.addCourseInfo(courseInfoEntity1)==0){
                return Result.error("课程信息插入失败");
            }
            id = courseInfoEntity1.getId();
        } else {
            id = courseInfoEntity.getId();
            CourseProjectInfoEntity  courseProjectInfoEntity = commonMapper.selectCourseProjectById(id);
            if(courseProjectInfoEntity!=null&&!courseProjectInfoEntity.getResponsibleTeacherId().equals(addCourseBo.getResponsibleTeacherId())){
                return Result.error("同一门课程的责任教师必须一样");
            }
        }
        CourseProjectInfoEntity courseProjectInfoEntity = new CourseProjectInfoEntity();
        courseProjectInfoEntity.setCourseInfoId(id);
        courseProjectInfoEntity.setProjectDate(addCourseBo.getProjectDate());
        courseProjectInfoEntity.setClassRome(addCourseBo.getClassRome());
        courseProjectInfoEntity.setSignInTime(addCourseBo.getSignInTime());
        courseProjectInfoEntity.setSignOutTime(addCourseBo.getSignOutTime());
        courseProjectInfoEntity.setResponsibleTeacherId(addCourseBo.getResponsibleTeacherId());
        courseProjectInfoEntity.setRemark(addCourseBo.getRemark());
        courseProjectInfoEntity.setDelFlag(ValidEnum.EFFECTIVE.getKey());
        courseProjectInfoEntity.setCreateBy(userEntity.getId());
        courseProjectInfoEntity.setCreateTime(DateUtils.getCurrentTime());
        courseProjectInfoEntity.setUpdateBy(userEntity.getId());
        courseProjectInfoEntity.setUpdateTime(DateUtils.getCurrentTime());
        if(courseMapper.addCourseProjectInfo(courseProjectInfoEntity)==0){
            return Result.error("课程项目信息插入失败");
        }
        CourseProjectTeacherInfoEntity courseProjectTeacherInfoEntity = new CourseProjectTeacherInfoEntity();
        courseProjectTeacherInfoEntity.setCourseProjectInfoId(courseProjectInfoEntity.getId());
        courseProjectTeacherInfoEntity.setTeacherRole(TeacherRole.TRAINING.getKey());
        for(String teacherId :addCourseBo.getTeacherId()){
            courseProjectTeacherInfoEntity.setTeacherId(teacherId);
            if(courseMapper.addCourseProjectTeacherInfo(courseProjectTeacherInfoEntity)==0){
                return Result.error("插入实训教师失败");
            }
        }
        courseProjectTeacherInfoEntity.setTeacherId(addCourseBo.getResponsibleTeacherId());
        courseProjectTeacherInfoEntity.setTeacherRole(TeacherRole.RESPONSIBLE.getKey());
        if(courseMapper.addResponsibleTeacherInfo(courseProjectTeacherInfoEntity)==0){
            return Result.error("插入责任教师失败");
        }
        return Result.success();
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public synchronized Result chooseCourseStudent(ChooseCourseStudentBo chooseCourseStudentBo, UserEntity userEntity) {
        DataCheckUtils.isNotBlank(chooseCourseStudentBo.getCourseProjectInfoId(), "课程项目ID不能为空!");
        DataCheckUtils.isValid(chooseCourseStudentBo.getTeamId().length!=0, "项目组ID不能为空!");
        if(commonMapper.selectCourseProjectByIds(chooseCourseStudentBo.getCourseProjectInfoId())==null){
            return Result.error("项目课程ID不存在");
        }
        String teamInfoIds = StringUtils.join(chooseCourseStudentBo.getTeamId(),",");
        List<UserEntity> userEntities = null;
        if(chooseCourseStudentBo.getStudentId().length==0){
            for(String teamInfoId: chooseCourseStudentBo.getTeamId()){
                TeamInfoEntity teamInfoEntity =commonMapper.getTeamById(teamInfoId);
                if(teamInfoEntity==null){
                    return Result.error("项目组ID"+teamInfoId+"不存在");
                }
                if(teamInfoEntity.getIsGraduation().equals(YesOrNoEnum.YES.getKey())){
                    return Result.error("项目组ID"+teamInfoId+"已结业");
                }
            }
            userEntities = commonMapper.selectTeamStudents(new SelectTeamStudent(teamInfoIds,""));
            if(userEntities.size()==0){
                return Result.error("所选项目组内没有学生");
            }
            CourseMember courseMember = new CourseMember();
            courseMember.setId(chooseCourseStudentBo.getCourseProjectInfoId());
            for(UserEntity userEntity1:userEntities){
                courseMember.setStudentId(userEntity1.getId());
                if(courseMapper.addCourseStudent(courseMember)==0){
                    return Result.error("添加失败");
                }
            }
        }else {
            String studentIds = StringUtils.join(chooseCourseStudentBo.getStudentId(),",");
            List<UserEntity> userEntities1 = commonMapper.selectTeamStudents(new SelectTeamStudent(teamInfoIds,studentIds));
            if(userEntities1.size()!=chooseCourseStudentBo.getStudentId().length){
                return Result.error("所选学生数目与项目组学生数目不和");
            }
            CourseMember courseMember = new CourseMember();
            courseMember.setId(chooseCourseStudentBo.getCourseProjectInfoId());
            for(String studentId:chooseCourseStudentBo.getStudentId()){
                courseMember.setStudentId(studentId);
                if(courseMapper.addCourseStudent(courseMember)==0){
                    return Result.error("添加失败");
                }
            }
        }
        return Result.success();
    }

    @Override
    public Result selectCourse(SelectCourseBo selectCourseBo, String id,UserEntity userEntity) {
            if (selectCourseBo.getProjectStartDate() != null && !selectCourseBo.getProjectStartDate().equals("") && selectCourseBo.getProjectEndDate() != null && !selectCourseBo.getProjectEndDate().equals("")) {
                int time=selectCourseBo.getProjectStartDate().compareTo(selectCourseBo.getProjectEndDate());
                if (time > 0) {
                    return Result.error("时间错误");
                }
                if ((!selectCourseBo.getProjectEndDate().matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
                        && !selectCourseBo.getProjectStartDate().matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
                        || selectCourseBo.getProjectEndDate().matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
                        && !selectCourseBo.getProjectStartDate().matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
                        || !selectCourseBo.getProjectEndDate().matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
                        && selectCourseBo.getProjectStartDate().matches("^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$")
                )) {
                    SelectCourseVo selectClassVo = new SelectCourseVo();
                    List<SelectCourseVo> selectCourseVos = new ArrayList<SelectCourseVo>();
                    selectCourseVos.add(selectClassVo);
                    return Result.success(new QueryReturnDate(PaginationUtils.getPaginationDate(selectCourseBo.getNumber(), selectCourseBo.getPage(), 0), selectCourseVos), "日期格式不正确");
                }
            } else if (((selectCourseBo.getProjectStartDate() == null || selectCourseBo.getProjectStartDate().equals(""))&& (selectCourseBo.getProjectEndDate() != null && !selectCourseBo.getProjectEndDate().equals("")))
                        ||((selectCourseBo.getProjectStartDate() != null && !selectCourseBo.getProjectStartDate().equals("")) && (selectCourseBo.getProjectEndDate() == null || selectCourseBo.getProjectEndDate().equals("")))) {
                return Result.error("时间有错误");
            }


        int number = 1;
        int page = 1;
        if(id==null){
            number = selectCourseBo.getNumber();
            page = selectCourseBo.getPage();
            //分页参数是否正确
            DataCheckUtils.isValid(PaginationUtils.isPagingParameters(number, page), "分页参数错误!");
        }
        String sid = "";
        String tid = "";
        if(userEntity.getRoleType().equals(RoleTypeEnum.TEACHER.getKey())){
            tid = userEntity.getId();
        }else if(userEntity.getRoleType().equals(RoleTypeEnum.STUDENT.getKey())){
            sid = userEntity.getId();
        }
        //查询项目组评分 mybatis
        List<Object> result = courseMapper.selectCourse(new SelectCourseQuery(PaginationUtils.getPageIndex(page, number),number, id, selectCourseBo.getProjectStartDate(),selectCourseBo.getProjectEndDate(),selectCourseBo.getCourseName(),selectCourseBo.getResponsibleTeacherId(),selectCourseBo.getTeacherId(),sid,tid));
        if(id!=null) {
            int sum = ((List<Integer>) result.get(1)).get(0);
            if(sum==0){
                return Result.error("班级不存在");
            }
            SelectCourseVo selectCourseVo =  ((List<SelectCourseVo>) result.get(0)).get(0);
            List<UserEntity> userEntity1 = commonMapper.getTeacherByIdss(selectCourseVo.getTeacherIds());
            List<Teacher> teachers = new ArrayList<Teacher>();
            for(UserEntity userEntity2:userEntity1){
                Teacher teacher = new Teacher(userEntity2.getId(),userEntity2.getName());
                teachers.add(teacher);
            }

            selectCourseVo.setTeacherId(teachers);
            return  Result.success(selectCourseVo);
        }
        int sum = ((List<Integer>) result.get(1)).get(0);
        List<SelectCourseVo> selectCourseVos = (List<SelectCourseVo>) result.get(0);
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(number, page, sum);
        //记录总数为0
        if (sum == 0) {
            SelectCourseVo selectClassVo = new SelectCourseVo();
            selectCourseVos.add(selectClassVo);
            return Result.success(new QueryReturnDate(paginationDate, selectCourseVos));
        }
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error("当前页大于总页数");
        for(SelectCourseVo selectCourseVo:selectCourseVos){
            List<UserEntity> userEntity1 = commonMapper.getTeacherByIdss(selectCourseVo.getTeacherIds());
            List<Teacher> teachers = new ArrayList<Teacher>();
            for(UserEntity userEntity2:userEntity1){
                Teacher teacher = new Teacher(userEntity2.getId(),userEntity2.getName());
                teachers.add(teacher);
            }

            selectCourseVo.setTeacherId(teachers);
        }
        return Result.success(new QueryReturnDate(paginationDate, selectCourseVos));
    }

    @Override
    public Result updateCourse(AddCourseBo addCourseBo, UserEntity userEntity) {
        //数据校验
        DataCheckUtils.isNotBlank(addCourseBo.getCourseProjectInfoId(), "课程项目ID不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getCourseName(), "课程名称不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getProjectDate(), "课程日期不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getClassRome(), "教室不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getSignInTime(), "签到时间不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getSignOutTime(), "签退时间不能为空!");
        DataCheckUtils.isNotBlank(addCourseBo.getResponsibleTeacherId(), "责任老师ID不能为空!");
        DataCheckUtils.isValid(addCourseBo.getTeacherId().length!=0, "实训老师ID不能为空!");
        DataCheckUtils.isValid(addCourseBo.getProjectDate().matches("([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))" ), "签到日期格式错误!");
        DataCheckUtils.isValid(addCourseBo.getSignInTime().matches("^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$"), "签到时间格式错误!");
        DataCheckUtils.isValid(addCourseBo.getSignOutTime().matches("^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$"), "签退时间格式错误!");
        DataCheckUtils.isValid(commonMapper.selectCourseProjectByIds(addCourseBo.getCourseProjectInfoId())!=null, "课程项目ID不存在!");

        CourseInfoEntity courseInfoEntity = commonMapper.selectCourseByNames(addCourseBo.getCourseName());
        if(courseInfoEntity!=null&&courseInfoEntity.getIsGraduation().equals(YesOrNoEnum.YES.getKey())){
            return Result.error("课程已经结束");
        }
        addCourseBo.setSignOutTime(addCourseBo.getSignOutTime()+":00");
        addCourseBo.setSignInTime(addCourseBo.getSignInTime()+":00");
        DataCheckUtils.isValid(DateUtils.compare(DateUtils.getCurrentTime(),addCourseBo.getProjectDate()+" "+addCourseBo.getSignInTime()), "课程日期不能小于当前日期!");
        DataCheckUtils.isValid(DateUtils.compare(addCourseBo.getProjectDate()+" "+addCourseBo.getSignInTime(),addCourseBo.getProjectDate()+" "+addCourseBo.getSignOutTime()), "结束时间不能小于开课时间!");

        int state = 0;
        for(String teacherId: addCourseBo.getTeacherId()){
            UserEntity userEntity1 = commonMapper.getTeacherById(teacherId);
            if(userEntity1==null){
                return Result.error("教师ID"+teacherId+"不存在");
            }
            if(teacherId.equals(addCourseBo.getResponsibleTeacherId())){
                state = 1;
            }
        }
        if(state==0){
            return Result.error("实训老师未在责任老师中");
        }
        if(courseMapper.selectCourseBy(addCourseBo)!=null){
            return Result.error("当前时间段教室被占有");
        }
        String id = "";
        if(courseInfoEntity==null) {
            CourseInfoEntity courseInfoEntity1 = new CourseInfoEntity();
            courseInfoEntity1.setCourseName(addCourseBo.getCourseName());
            courseInfoEntity1.setDelFlag(ValidEnum.EFFECTIVE.getKey());
            courseInfoEntity1.setCreateBy(userEntity.getId());
            courseInfoEntity1.setCreateTime(DateUtils.getCurrentTime());
            courseInfoEntity1.setUpdateBy(userEntity.getId());
            courseInfoEntity1.setUpdateTime(DateUtils.getCurrentTime());
            if(courseMapper.addCourseInfo(courseInfoEntity1)==0){
                return Result.error("课程信息插入失败");
            }
            id = courseInfoEntity1.getId();
        } else {
            id = courseInfoEntity.getId();
            CourseProjectInfoEntity  courseProjectInfoEntity = commonMapper.selectCourseProjectById(id);
            if(courseProjectInfoEntity!=null&&!courseProjectInfoEntity.getResponsibleTeacherId().equals(addCourseBo.getResponsibleTeacherId())){
                return Result.error("同一门课程的责任教师必须一样");
            }
         }
        CourseProjectInfoEntity courseProjectInfoEntity = new CourseProjectInfoEntity();
        courseProjectInfoEntity.setCourseInfoId(id);
        courseProjectInfoEntity.setId(addCourseBo.getCourseProjectInfoId());
        courseProjectInfoEntity.setProjectDate(addCourseBo.getProjectDate());
        courseProjectInfoEntity.setClassRome(addCourseBo.getClassRome());
        courseProjectInfoEntity.setSignInTime(addCourseBo.getSignInTime());
        courseProjectInfoEntity.setSignOutTime(addCourseBo.getSignOutTime());
        courseProjectInfoEntity.setResponsibleTeacherId(addCourseBo.getResponsibleTeacherId());
        courseProjectInfoEntity.setRemark(addCourseBo.getRemark());
        courseProjectInfoEntity.setDelFlag(ValidEnum.EFFECTIVE.getKey());
        courseProjectInfoEntity.setUpdateBy(userEntity.getId());
        courseProjectInfoEntity.setUpdateTime(DateUtils.getCurrentTime());
        if(courseMapper.UpdateCourseProjectInfo(courseProjectInfoEntity)==0){
            return Result.error("课程项目信息修改失败");
        }
        int row1 = courseMapper.deleteCourseTeacherInfo(addCourseBo.getCourseProjectInfoId());
        if(row1==0){
            return Result.error("更新教师失败");
        }
        CourseProjectTeacherInfoEntity courseProjectTeacherInfoEntity = new CourseProjectTeacherInfoEntity();
        courseProjectTeacherInfoEntity.setCourseProjectInfoId(courseProjectInfoEntity.getId());
        courseProjectTeacherInfoEntity.setTeacherRole(TeacherRole.TRAINING.getKey());
//        String[] str = addCourseBo.getTeacherId().split("\\,");
//        List<String> a= (List<String>)Arrays.asList(str);
//        courseProjectTeacherInfoEntity.setTeacherId(a);
        for(String teacherId :addCourseBo.getTeacherId()){
           courseProjectTeacherInfoEntity.setTeacherId(teacherId);
           if(courseMapper.addCourseProjectTeacherInfo(courseProjectTeacherInfoEntity)==0){
               return Result.error("插入实训教师失败");
           }
        }
//        if(courseMapper.addCourseProjectTeacherInfo(courseProjectTeacherInfoEntity)==0){
////                return Result.error("插入实训教师失败");
////            }
        courseProjectTeacherInfoEntity.setTeacherId(addCourseBo.getResponsibleTeacherId());
        courseProjectTeacherInfoEntity.setTeacherRole(TeacherRole.RESPONSIBLE.getKey());
        if(courseMapper.addResponsibleTeacherInfo(courseProjectTeacherInfoEntity)==0){
            return Result.error("插入责任教师失败");
        }
        return Result.success();
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public synchronized Result deleteCourse(CourseBo courseBo, UserEntity userEntity) {
        DataCheckUtils.isNotBlank(courseBo.getCourseProjectInfoId(), "课程项目ID不能为空!");
        CourseProjectInfoEntity courseProjectInfoEntity = commonMapper.selectCourseProjectByIds(courseBo.getCourseProjectInfoId());
        DataCheckUtils.isValid(courseProjectInfoEntity!=null, "课程项目ID不存在!");
        CourseInfoEntity courseInfoEntity= commonMapper.selectCourseById(courseProjectInfoEntity.getCourseInfoId());
        if(courseInfoEntity.getIsGraduation().equals(YesOrNoEnum.YES.getKey())){
            return Result.error("课程已经结束不允许删除");
        }
        CourseProjectInfoEntity courseProjectInfoEntity1 = new CourseProjectInfoEntity();
        courseProjectInfoEntity1.setUpdateBy(userEntity.getId());
        courseProjectInfoEntity1.setUpdateTime(DateUtils.getCurrentTime());
        courseProjectInfoEntity1.setDelFlag(ValidEnum.INVALID.getKey());
        courseProjectInfoEntity1.setId(courseBo.getCourseProjectInfoId());
        int row = courseMapper.deleteCourseProjectInfo(courseProjectInfoEntity1);
        if(row==0){
            return Result.error("删除失败");
        }
        int row1 = courseMapper.deleteCourseTeacherInfo(courseBo.getCourseProjectInfoId());
        if(row1==0){
            return Result.error("删除失败");
        }
        String id   = commonMapper.selectCourseNumber(courseBo.getCourseProjectInfoId());
        if(id!=null){
            int row2 = courseMapper.deleteCourseMemberInfo(courseBo.getCourseProjectInfoId());
            if(row2==0){
                return Result.error("学生删除失败");
            }
        }
        return Result.success();
    }

    @Override
    public Result seeStudent(SeeStudentBo seeStudentBo) {
        int number = seeStudentBo.getNumber();
        int page = seeStudentBo.getPage();
            //分页参数是否正确
        if(PaginationUtils.isPagingParameters(number, page)==false){
            SelectTeamStudentVo selectTeamStudentVo = new SelectTeamStudentVo();
            List<SelectTeamStudentVo> selectTeamStudentVos = new ArrayList<SelectTeamStudentVo>();
            selectTeamStudentVos.add(selectTeamStudentVo);
            return Result.success(new QueryReturnDate(PaginationUtils.getPaginationDate(1,1,0), selectTeamStudentVos),"分页参数错误");
        };
        //DataCheckUtils.isNotBlank(selectTeamStudentBo.getIsGraduation(), "项目组是否结业不能为空!");
        //DataCheckUtils.isValid(YesOrNoEnum.getEnumValue(selectTeamStudentBo.getIsGraduation()) != null,"项目组是否结业的状态值不存在");
        if(seeStudentBo.getCourseProjectInfoId()==null||seeStudentBo.getCourseProjectInfoId().equals("")){
            SelectTeamStudentVo selectTeamStudentVo = new SelectTeamStudentVo();
            List<SelectTeamStudentVo> selectTeamStudentVos = new ArrayList<SelectTeamStudentVo>();
            selectTeamStudentVos.add(selectTeamStudentVo);
            return Result.success(new QueryReturnDate(PaginationUtils.getPaginationDate(seeStudentBo.getNumber(),seeStudentBo.getPage(),0), selectTeamStudentVos),"项目课程ID不能为空");
        }
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(seeStudentBo.getNumber(), seeStudentBo.getPage()), "分页参数错误!");
        List<Object> result = courseMapper.SeeStudent(new SeeStudentQuery(PaginationUtils.getPageIndex(page, number),number,seeStudentBo.getCourseProjectInfoId(),seeStudentBo.getUserNo(),seeStudentBo.getName(),seeStudentBo.getCollegesId(),seeStudentBo.getMajorId(),seeStudentBo.getYearInfoId(),seeStudentBo.getClassBaseInfoId()));
        int sum = ((List<Integer>) result.get(1)).get(0);
        List<SelectTeamStudentVo> selectTeamStudentVos = (List<SelectTeamStudentVo>) result.get(0);
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(number, page, sum);
        //记录总数为0
        if (sum == 0) {
            SelectTeamStudentVo selectTeamStudentVo = new SelectTeamStudentVo();
            selectTeamStudentVos.add(selectTeamStudentVo);
            return Result.success(new QueryReturnDate(paginationDate, selectTeamStudentVos));
        }
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error("当前页大于总页数");
        return Result.success(new QueryReturnDate(paginationDate, selectTeamStudentVos));
    }
}
